//Typedefs
typedef unsigned char     T_UBYTE;
typedef unsigned int      T_UWORD;

//Define
#define	ReverseLightsOnDelay_cfg	3
#define	RUN							1
#define	ACTIVE						1
#define	INACTIVE					0
#define	ON							1
#define	OFF							0

//SM Nested
typedef enum
{
	RESET
	REVERSE_LIGTHS_OFF,
	typedef enum
	{
		ON_TIMING_DELAY,
		REVERSE_LIGHTS_ON
	}REVERSE_LIGTHS_ALLOWED;

}SM_REVERSE_LIGHTS;

//Variable declaration
T_UBYTE	Ignition_Status;
T_UBYTE	Reverse_Status;
T_UBYTE	Reverse_Lights_Cmd;
T_UBYTE	Cycle_Counter;

void vfn_reverse_lights(void)
{
	static SM_REVERSE_LIGHTS sm_reverse_ligths = RESET;
	static REVERSE_LIGTHS_ALLOWED sm_reverse_ligths_allowed = ON_TIMING_DELAY;
	
	switch (sm_reverse_ligths)
	{
		case RESET:
		{
			Reverse_Ligths = OFF;
			Cycle_Counter = 0;
			sm_reverse_ligths = REVERSE_LIGTHS_OFF;
		}
		
		case REVERSE_LIGHTS_OFF:
		{
			if((Ignition_Status == RUN) && (Reverse_Status == ACTIVE))
			{
				Cycle_Counter = 0;
				sm_reverse_ligths = REVERSE_LIGTHS_ALLOWED;
			}
			else if((Ignition_Status != RUN) || (Reverse_Status == INACTIVE))
			{
				Reverse_Ligths_Cmd = OFF
				sm_reverse_ligths = REVERSE_LIGTHS_OFF;
			}
			else{}
			break;
		}
		
		case REVERSE_LIGTHS_ALLOWED:
		{
			switch (sm_reverse_ligths_allowed)
			{
				case ON_TIMING_DELAY:
				{	
					if((Ignition_Status != RUN) || (Reverse_Status == INACTIVE))
					{
						sm_reverse_ligths = REVERSE_LIGHTS_OFF;
						Reverse_Ligths_Cmd =  OFF;
					}
					else
					{
						if(Cycle_Counter < ReverseLightsOnDelay_cfg)
						{
							Cycle_Counter++;
							sm_reverse_ligths_allowed = ON_TIMING_DELAY;
						}
						else
						{
							Reverse_Ligths_Cmd =  ON;
							sm_reverse_ligths_allowed = REVERSE_LIGHTS_ON;
						}
					}
				break;
				}
				case REVERSE_LIGHTS_ON:
				{
					if((Ignition_Status == RUN) && (Reverse_Status == ACTIVE))
					{
						Reverse_Ligths_Cmd = ON;
						sm_reverse_ligths_allowed = REVERSE_LIGTHS_ON;
					}
					else if if((Ignition_Status != RUN) || (Reverse_Status == INACTIVE))
					{
						sm_reverse_ligths = REVERSE_LIGHTS_OFF;
						Reverse_Ligths_Cmd =  OFF;
					}
					else
					{
					}
					break;
				}
				default:
				break;
			}
		}
		default:
		break;
	}
}


 